"""
@-*- coding: utf-8 -*-
@ python: 3.12.3
@ 创建者: JacksonCode
@ 创建时间: 2025/8/20
"""

import time
import hashlib

import requests
import base64
import json
from Crypto.Util.Padding import pad, unpad

from Crypto.Cipher import AES

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/json;charset=UTF-8",
    "Origin": "https://ggzyfw.fujian.gov.cn",
    "Pragma": "no-cache",
    "Referer": "https://ggzyfw.fujian.gov.cn/business/list",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
    # "portal-sign": "d1cd8e40e7a4f549fbe6618ee0bad055",
}
url = "https://ggzyfw.fujian.gov.cn/FwPortalApi/Trade/TradeInfo"


def get_portal_sign(data):
    new_data = {}
    for k, v in data.items():
        if v == "" or v == 0:
            continue
        new_data[k] = str(v)

    sorted_data = sorted(new_data.items())

    result = ""
    for k, v in sorted_data:
        # print(f"key:{k}, val:{v} key type is {type(k)} val type is {type(v)}")
        result += k + v
    # 'B3978D054A72A7002063637CCDF6B2E5BeginTime2025-02-20 00:00:00EndTime2025-08-20 23:59:59GGTYPE1KINDGCJSpageNo6pageSize20timeType6total2926ts1755668180370'
    ra = "B3978D054A72A7002063637CCDF6B2E5"
    result = ra + result
    # B3978D054A72A7002063637CCDF6B2E5BeginTime2025-02-20 00:00:00EndTime2025-08-20 23:59:59GGTYPE1KINDGCJSpageNo5pageSize20timeType6total2926ts1755668261554
    # print(result)

    # 创建 MD5 对象
    md5_hash = hashlib.md5()
    # 更新哈希对象（需要编码为字节）
    md5_hash.update(result.encode('utf-8'))
    # 获取十六进制哈希值
    sign = md5_hash.hexdigest()
    return sign


def main():
    for i in range(1, 11):
        data = {
            "pageNo": i,
            "pageSize": 20,
            "total": 2926,
            "AREACODE": "",
            "M_PROJECT_TYPE": "",
            "KIND": "GCJS",
            "GGTYPE": "1",
            "PROTYPE": "",
            "timeType": "6",
            "BeginTime": "2025-02-20 00:00:00",
            "EndTime": "2025-08-20 23:59:59",
            "createTime": "",
            "ts": int(time.time() * 1000)
        }

        headers['portal-sign'] = get_portal_sign(data)
        # print(headers)
        time.sleep(1)
        data = json.dumps(data, separators=(',', ':'))
        response = requests.post(url, headers=headers, data=data)
        base64_encrypt_data = response.json().get("Data")
        encrypt_data = base64.b64decode(base64_encrypt_data)
        # print(f'encrypt_data: {encrypt_data}')
        key = 'EB444973714E4A40876CE66BE45D5930'.encode()
        iv = 'B5A8904209931867'.encode()
        aes = AES.new(key, AES.MODE_CBC, iv)
        data = aes.decrypt(encrypt_data)
        data = unpad(data, 16).decode()
        # print(data)
        print_detail(data)
        # print(type(data))  # str


def print_detail(data):
    json_data = json.loads(data)
    Table = json_data["Table"]
    # print(Table)
    for item in Table:
        print(item.get("NAME"))


if __name__ == '__main__':
    main()
